/*
	序列号添加
*/

//登录信息
var adminLoginedInfo = Cookies.getJSON('kplus_ad_logined_info');
var DICT_AGT = JSON.parse(sessionStorage.getItem('kplus_web_dict_agt'));
var DICT_ADMIN = JSON.parse(sessionStorage.getItem('kplus_web_dict_admin'));
var DICT_PNAME  = JSON.parse(sessionStorage.getItem('kplus_web_dict_pName'));

//dom
$(function(){
	layui.use(['layer', 'form'], function(){
		var layer = layui.layer,
			form = layui.form;

		//ajax data	
		var ajaxData = {
			aId: adminLoginedInfo.aId,
			aToken : adminLoginedInfo.aToken
		}		
		//字典
		createDicts(DICT_AGT, DICT_PNAME);
		form.render();

		form.on('submit(add)', function(data){
			//console.log(data);
			
			layer.confirm(MESSAGES.confirm_add_psn, {title:'提示'}, function(index){
				//判断数量
				if(data.field.num > 100 || data.field.num < 1){
					layer.msg('生成序列号数量错误！', {time: 2000});
					return false;
				}else{
					console.log(data.field);
					addNewPsn(data.field);
				}
				layer.close(index); //关闭询问框
			});
			function addNewPsn(data){
				var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
				var _ajaxData = $.extend({}, ajaxData, data);
				$.ajax({
					url: BASE_SERVICEURL + URlS.psn,
					type:'PUT', 
					data: _ajaxData,
					success: function(responseData){
						if(responseData.msg.code == 200){
							layer.close(loadIndex);
							layer.msg(responseData.data.msg, {time: 2000, end: function(){
								window.parent.location.reload();
							}});
						}else{
							layer.close(loadIndex);
							layer.msg(responseData.msg.msg, {time: 2000});
						}
					},
					error: function(responseData){
						layer.close(loadIndex);
						layer.msg(MESSAGES.service_error, {time:2000, end: function(){
							location.href = './500.html';
						}})
					}
				})
			}
			return false;
		})
	});
})



